<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="起止时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="报修申请单号" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修项目" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维修班组" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="完成" prop="roleName">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用单位" prop="roleName">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据号" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button @click="handleAdd" size="mini">新增</el-button>
        <el-button @click="handleView" size="mini">查看</el-button>   
        <el-button @click="handleEdit" size="mini">编辑</el-button> 
        <el-button @click="handleDelete" size="mini">删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange1">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据号" prop="roleName" />
      <el-table-column label="设备编号" prop="roleKey"  />
      <el-table-column label="设备名称" prop="roleSort" />
      <el-table-column label="日期" prop="roleSort" />
      <el-table-column label="保修申请单号" prop="roleSort" width="100" />
      <el-table-column label="制单人" prop="roleSort" />
      <el-table-column label="维修班组" prop="roleSort" />
      <el-table-column label="使用单位" prop="roleSort" />
      <el-table-column label="故障地点" prop="roleSort" />
      <el-table-column label="维修项目" prop="roleSort" />
      <el-table-column label="维修内容" prop="roleSort" />
      <el-table-column label="下次保养时间" prop="roleSort" width="100" />
      <el-table-column label="备注" prop="roleSort" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新增编辑弹窗 -->
    <el-dialog class="scrollbar" :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :rules="rules" :model="form" label-width="110px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单据号" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="日期" prop="q">
              <el-date-picker
                :readonly="isReadOnly"
                style="width: 100%;"
                v-model="form.q"
                type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="报修申请单号" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用单位" prop="q">
              <el-select style="width: 100%;" v-model="form.w" :readonly="isReadOnly" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="维修班组" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="故障地点" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="维修项目" prop="q">
              <el-select style="width: 100%;" v-model="form.w" :readonly="isReadOnly" placeholder="请选择">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修内容" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="备注" prop="q">
              <el-input v-model="form.q" :readonly="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="明细" name="first"></el-tab-pane>
        <el-tab-pane label="配件" name="second"></el-tab-pane>
      </el-tabs>
      <template v-if="activeName==='first'">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button @click="handleAdd1" size="mini">新增</el-button>
            <el-button @click="handleDelete1" size="mini">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="listDetail1" @selection-change="handleSelectionChange2">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="维修项目" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="维修内容" prop="w">
            <template slot-scope="scope">
              <el-input v-model="scope.row.w" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="维修结果" prop="e">
            <template slot-scope="scope">
              <el-input v-model="scope.row.e" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-if="activeName==='second'">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button @click="handleAdd2" size="mini">新增</el-button>        
            <el-button @click="handleDelete2" size="mini">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="listDetail2" @selection-change="handleSelectionChange3">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="行号" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="是否收费" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="材料名称" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="材料类别" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="材料规格" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="q">
            <template slot-scope="scope">
              <el-input v-model="scope.row.q" placeholder="请输入"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="title !== '查看'" type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRole } from "@/api/system/role";
import { GetDateStr } from "@/utils/index"

export default {
  data() {
    return {
      // 时间筛选
      radio: '',
      // 下拉数据
      options: [],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined
      },
      // 拉黑时间
      dateRange: [],
      ids1: [],
      ids2: [],
      ids3: [],
      // tab
      activeName: 'first',
      // 是否只读
      isReadOnly: false,
      // 表单
      title: '新增',
      open: false,
      rules: {},
      form: {},
      // 明细配件字段
      listDetail1: [],
      listDetail2: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 选择
    handleSelectionChange1() {

    },
    handleSelectionChange2() {

    },
    handleSelectionChange3() {

    },
    // 表格添加行类名
    tableRowClassName({row, rowIndex}) {
      
    },
    // tab改变
    handleClick() {
      this.queryDetail.pageNum = 1
    },
    // 时间筛选
    changeRedio(val) {
      if (val === '今天') {
        this.dateRange = [GetDateStr(0), GetDateStr(0)]
      } else if (val === '整周') {
        this.dateRange = [GetDateStr(-7), GetDateStr(-1)]
      } else if (val === '本月') {
        const dd = new Date()
        const year = dd.getFullYear()
        const month = dd.getMonth() + 1// 获取当前月份的日期
        const monthStartDate = new Date(year, month - 1, 1)
        const monthEndDate = new Date(year, month, 1)
        const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
        this.dateRange = [`${year}-${month}-01`, `${year}-${month}-${days}`]
      } else if (val === '整月') {
        this.dateRange = [GetDateStr(-30), GetDateStr(-1)]
      }
      this.getList();
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      listRole(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.list = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 重置
    reset() {
      this.form = {}
      this.listDetail1 = []
      this.listDetail2 = []
      this.isReadOnly = false
    },
    // 弹窗新增编辑查看删除
    handleAdd() {
      this.reset()
      this.title = '新增'
      this.open = true
    },
    handleView() {
      this.reset()
      this.title = '查看'
      this.isReadOnly = true
      this.open = true
    },
    handleEdit() {
      this.reset()
      this.title = '编辑'
      this.open = true
    },
    handleDelete() {
      this.$modal
        .confirm("确定要删除该记录吗？")
        .then(() => {
          
        })
    },
    // 明细配件新增
    handleAdd1() {
      this.listDetail1.push({ q: '',w: '',e:''})
    },
    handleAdd2() {
      this.listDetail2.push({ q: ''})
    },
    // 表单确定
    submit() {
      
    },
    // 表单取消
    cancel() {
      this.open = false
    }
  }
};
</script>
